<?php
class QAdminUser
{
	static $_table = 'admins';
	
	static function search($p_strCountry, $p_intGroupId, $p_strUsername)
	{
		$strQuery	= '
			SELECT admin_id, admin_username, group_id, admin_createdate, admin_firstname, admin_lastname, country, email, group_leader, admin_isactive, admin_lastlogin
			FROM '.self::$_table.'
			WHERE 1=1
		';
		if($p_strCountry){
			$strQuery	.= ' AND LOWER(country)="'.strtolower($p_strCountry).'"';
		}
		if($p_intGroupId){
			$strQuery	.= ' AND group_id="'.intval($p_intGroupId).'"';
		}
		
		if($p_strUsername){
			$strQuery	.= ' AND LOWER(admin_username)="'.strtolower(trim($p_strUsername)).'"';
		}
		
		$strQuery	.= ' ORDER BY group_id, admin_createdate ASC';
		$db 		= Zend_Registry::get('db');
		$resultValue = $db->fetchAll($strQuery);
		$db->closeConnection();
		
		return $resultValue;
	}
	
	static function get_by_id($p_intId)
	{
		$strQuery	= 'SELECT * FROM '.self::$_table.' WHERE admin_id='.intval($p_intId);
		$db 		= Zend_Registry::get('db');
		$arrResult	=  $db->fetchAll($strQuery);
		$db->closeConnection();
		return isset($arrResult[0]) ? $arrResult[0] : null;
	}

	static function get_admins_temp()
	{
		$strQuery	= 'Select * From admins_temp where admin_username <> ""';
		$db 		= Zend_Registry::get('db');
		$arrResult	=  $db->fetchAll($strQuery);
		$db->closeConnection();
		return $arrResult;
	}
	static function getbyUsername($p_userName)
	{
		$strQuery	= 'SELECT * FROM '.self::$_table.' WHERE admin_username="'. $p_userName . '";';
		$db 		= Zend_Registry::get('db');
		$arrResult	=  $db->fetchAll($strQuery);
		$db->closeConnection();
		return $arrResult;
	}
		
	static function updatePassword($p_userName,$strPassOld, $strPassNew )
	{
		
		$arrResult	= self::getbyUsername($p_userName);	
		$arrResult[0]["admin_pwd"] = $strPassNew;
		
		$db 		= Zend_Registry::get('db');
		$result = $db->update(self::$_table, $arrResult[0], 'admin_id='.intval($arrResult[0]['admin_id']));
		$db->closeConnection();
		//Insert to table dragontool
		if($result)
		{
			$objAdminDragonTool = new QAdminUserDragonTool();
			$result = $objAdminDragonTool->updatePassword($p_userName,$strPassOld, $strPassNew);
		}
		
		return $result;
	}
	
	public function insert($p_arrParams)
	{
		$db	= Zend_Registry::get('db');
		$result =$db->insert(self::$_table, $p_arrParams); 
		$db->closeConnection();
		return $result;
	}
	
	public function update($p_arrParams, $p_intId)
	{
		$db	= Zend_Registry::get('db');
		$result = $db->update(self::$_table, $p_arrParams, 'admin_id='.intval($p_intId));
		$db->closeConnection();
		//Insert to table dragontool
		if($result)
		{
			$objAdminDragonTool = new QAdminUserDragonTool();
			$result = $objAdminDragonTool->update($p_arrParams, $p_arrParams['admin_username']);
		}
		return $result;
	}
	
	static function updateUpload()
	{
		$strPass = md5("123456");
		$now =getdate();
		$today = date('Y-m-d H:i:s', mktime($now['hours'], $now['minutes'], $now["seconds"], $now['mon'], $now['mday'], $now['year']));
		$db	= Zend_Registry::get('db');
		$p_arrParams = array(
			'admin_pwd' => $strPass,
			'admin_createdate' => $today
			);
		
		//Insert to DragonTool Admin Table
		self::updateUploadDragonTool($strPass, $today);
		
		//Update Username  to Email field
		$strQuery	= 'Update '.self::$_table.' SET email = admin_username WHERE email="";';
		$db 		= Zend_Registry::get('db');
		$arrResult	=  $db->execute($strQuery);
		
		$resultValue = $db->update(self::$_table, $p_arrParams, 'admin_pwd=""');
		$db->closeConnection();
		return $resultValue;
	}
	
	// Insert data to Table Admin Dragon tool when user upload CSV.
	static function updateUploadDragonTool($strPassWord, $today)
	{
		$db	= Zend_Registry::get('db');
		$strQuery	= 'SELECT * FROM '.self::$_table.' WHERE admin_pwd="";';
		$arrResult	=  $db->fetchAll($strQuery);
		
		$objAdminDragonTool = new QAdminUserDragonTool();
		foreach ($arrResult as $value)
		{
			$value['admin_pwd'] = $strPassWord;
			$value['admin_createdate'] = $today;
			$value['email'] = $value['admin_username'];
			$result = $objAdminDragonTool->insert($value);
		}
		$db->closeConnection();
	}
}
?>